<template>
  <div>
    <template>
      <div class="dashboard-container">
        <div class="app-container">
          <page-tools :is-show="true">
            <span slot="before">共166条记录</span>
            <template slot="after">
              <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
              <el-button size="small" type="danger" @click="exportExcel">导出</el-button>
              <el-button size="small" type="primary" :disabled="!isPermission('POINT-USER-ADD')" @click="addEmployees">新增员工</el-button>
            </template>
          </page-tools>
          <!-- 放置表格和分页 -->
          <el-card>
            <el-table border :data="employeesData">
              <el-table-column label="序号" sortable="" type="index" />
              <el-table-column>
                <template slot-scope="{row}">
                  <img v-imgError="require('@/assets/common/lzs.jpg')" :src="row.staffPhoto" class="staffPhoto" @click="handleqrcode(row.staffPhoto)">
                </template>
              </el-table-column>
              <el-table-column label="姓名" sortable="" prop="username" />
              <el-table-column label="工号" sortable="" prop="workNumber" />
              <el-table-column
                label="聘用形式"
                sortable=""
                prop="formOfEmployment"
                :formatter="formatter"
              />
              <el-table-column
                label="
                部门"
                sortable=""
                prop="departmentName"
              />
              <el-table-column label="入职时间" sortable="">
                <template slot-scope="{row}">
                  {{ row.timeOfEntry | formatDate }}
                </template>
              </el-table-column>
              <el-table-column label="账户状态" sortable="" prop="enableState">
                <template slot-scope="{row}">
                  <el-switch :value="row.enableState==1" />
                </template>
              </el-table-column>>
              <el-table-column label="操作" sortable="" fixed="right" width="280">
                <template slot-scope="{row}">
                  <el-button type="text" size="small" @click="$router.push('employees/detail/'+row.id)">查看</el-button>
                  <el-button type="text" size="small">转正</el-button>
                  <el-button type="text" size="small">调岗</el-button>
                  <el-button type="text" size="small">离职</el-button>
                  <el-button type="text" size="small" @click="setRoles(row.id)">角色</el-button>
                  <el-button type="text" size="small" @click="delEmployees(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                layout="total, sizes, prev, pager, next, jumper"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="page.size"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
              />
            </el-row>
          </el-card>
          <Addemployees :show-dialog.sync="showDialog" @addemployees="isOk" />
          <!-- 二维码 -->
          <el-dialog title="二维码" :visible.sync="showCodeDialog">
            <el-row type="flex" justify="center">
              <canvas ref="myCanvas" />
            </el-row>
          </el-dialog>
          <!-- 角色 -->
          <RolesList ref="roles" :show-roles.sync="showRoles" :role-id="roleId" />
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import { delemployees, employeesDetail } from '@/api/employees'
// 枚举使用
import Emunemploy from '@/api/constant/employees' // 数据
import { enumEmploy } from '@/utils/index' // 方法
// 组件
import Addemployees from './components/addEmployees'
import RolesList from './components/rolesList'
// 过滤器在函数中不能使用,所以直接引入当普通函数用
import { formatDate } from '@/filter'
// 二维码库
import QRCode from 'qrcode'
export default {
  components: {
    Addemployees,
    RolesList
  },
  data() {
    return {
      // 角色
      showRoles: false,
      roleId: '',
      // 二维码
      showCodeDialog: false,
      employeesData: [],
      total: null,
      page: {
        page: 1,
        size: 5
      },
      showDialog: false
    }
  },
  created() {
    this.getEmployees()
  },
  methods: {
    // 获取数据
    async getEmployees() {
      const { rows, total } = await employeesDetail(this.page)
      this.employeesData = rows
      this.total = total
    },
    // 枚举
    formatter(row, column, cellValue, index) {
      return enumEmploy(Emunemploy.hireType, cellValue)
    },
    formatterstate(row, column, cellValue, index) {
      return enumEmploy(Emunemploy.stausInfos, cellValue)
    },
    // 更改页码
    handleCurrentChange(value) {
      this.page.page = value
      this.getEmployees()
    },
    // 更爱数据显示数量
    handleSizeChange(value) {
      this.page.size = value
      this.getEmployees()
    },
    // 删除
    async delEmployees(id) {
      try {
        await this.$confirm('您确定需要删除员工?')
        await delemployees(id)
        this.getEmployees()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 点击新增
    addEmployees() {
      this.showDialog = true
    },
    // 点击新增的确定
    isOk() {
      this.getEmployees()
    },
    // 点击导出
    exportExcel() {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      import ('@/vendor/Export2Excel').then(async item => {
        const { rows } = await employeesDetail({ page: 1, size: this.total })
        const res = rows.map(item => {
          return this.exportData(item, userRelations)
        })
        item.export_json_to_excel({
          header: Object.keys(userRelations),
          data: res
        })
      })
    },
    // 把data的内容抽取出来
    exportData(data, userRelations) {
      const res = []
      for (const key in userRelations) {
        if (key === '入职日期' || key === '转正日期') {
          data[userRelations[key]] = formatDate(data[userRelations[key]])
        }
        res.push(data[userRelations[key]])
      }
      return res
    },
    // 点击图片生成二维码
    handleqrcode(img) {
      if (img) {
        this.showCodeDialog = true
        this.$nextTick(() => {
          QRCode.toCanvas(this.$refs.myCanvas, img)
        })
      }
    },
    // 点击角色
    async setRoles(id) {
      this.roleId = id
      await this.$refs.roles.roleListById(id)
      this.showRoles = true
    }
  }

}
</script>
<style scoped>
.staffPhoto {
  width: 80px;
  height: 80px;
  object-fit: cover;
}
</style>
